package com.car.service.upload.impl;


import java.awt.Image;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.car.bean.UploadObj;
import com.car.bean.UploadPathObj;
import com.car.service.upload.AbstractUploadFile;
import com.car.service.upload.FileUploadService;
import com.car.util.Config;

public class FileUploadServiceImpl extends AbstractUploadFile implements
		FileUploadService {

	@Override
	public UploadObj uploadFile(CommonsMultipartFile file, String model_path)
			throws IOException {
		UploadObj obj = super.uploadFile(file.getInputStream(),
				file.getOriginalFilename(), model_path);
		return obj;
	}

	@Override
	public UploadObj uploadFile(FileInputStream input, String fileName,
			String model_path) {
		InputStream in = new BufferedInputStream(input);
		UploadObj obj = super.uploadFile(in, fileName, model_path);
		return obj;
	}

	@Override
	public UploadObj uploadFile(File file, String model_path)
			throws FileNotFoundException {
		InputStream in = new BufferedInputStream(new FileInputStream(file));
		UploadObj obj = super.uploadFile(in, file.getName(), model_path);
		return obj;
	}

	@Override
	public UploadObj uploadImgFile(CommonsMultipartFile file, int img_type,
			String model_path) throws IOException {
		String ext = getFileExt(file.getOriginalFilename());
		UploadObj obj = null;
		if (super.isImg(ext)) {
			obj = super.uploadFile(file.getInputStream(),
					file.getOriginalFilename(), model_path);
		}
		return obj;
	}

	@Override
	public UploadObj uploadImgFile(CommonsMultipartFile file, int x, int y,
			int wid, int hig, int img_type, String model_path)
			throws IOException {
		String ext = getFileExt(file.getOriginalFilename());
		UploadObj obj = null;
		if (super.isImg(ext)) {
			obj = super.uploadFile(file.getInputStream(),
					file.getOriginalFilename(), model_path);
			if (img_type == AbstractUploadFile.SMALL_IMG) {
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createSmallImg(f, wid, hig);
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
			}
			if (img_type == AbstractUploadFile.SUB_IMG) {
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createCutImg(f, x, y, wid, hig);
				obj.setSub_img_path(pathObj.getRealPath());
				obj.setRelativeSubImgPath(pathObj.getRelativePath());
			}
			if (img_type == AbstractUploadFile.MULTI_THUMBNAIL) {
				// 压缩多套图
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createSmallImg(f, 90, 60, 1);
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
				// 再压三套,用“wid_hig.jpg”来定义文件名
				super.createSmallImg(f, 150, 100, 1);
				super.createSmallImg(f, 300, 200, 1);
				super.createSmallImg(f, 600, 400, 1);
				super.createSmallImg(f, 800, 600, 1);
				// 测试small用代码，后续需要去掉
				// super.createSmallImg(f,150,100);
			}
		}
		return obj;
	}
	public UploadObj uploadappImgFile(CommonsMultipartFile file, int x, int y,
			int wid, int hig, int img_type, String model_path)
			throws IOException {
		String ext = getFileExt(file.getOriginalFilename());
		UploadObj obj = null;
		if (super.isImg(ext)) {
			obj = super.uploadFile(file.getInputStream(),
					file.getOriginalFilename(), model_path);
			if (img_type == AbstractUploadFile.MULTI_THUMBNAIL) {
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createappSmallImg(f, 200, 200, 1);
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
			}
		}
		return obj;
	}
	@Override
	public UploadObj uploadImgFile(CommonsMultipartFile file, String model_path)
			throws IOException {
		return this.uploadImgFile(file, AbstractUploadFile.SMALL_IMG,
				model_path);
	}

	@Override
	public UploadObj uploadImgFile(File file, String model_path)
			throws IOException {
		return this.uploadImgFile(file, AbstractUploadFile.SMALL_IMG,
				model_path);
	}

	@Override
	public UploadObj uploadImgFile(File file, int img_type, String model_path)
			throws IOException {
		String ext = super.getFileExt(file.getName());
		UploadObj obj = null;
		if (super.isImg(ext)) {
			obj = this.uploadFile(file, model_path);
			if (img_type == AbstractUploadFile.SMALL_IMG) {
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createSmallImg(f);

				// obj.setSmall_img_path(super.createSmallImg(f));
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
			}
		}
		return obj;
	}

	@Override
	public UploadObj uploadImgFile(File file, int x, int y, int wid, int hig,
			int img_type, String model_path) throws IOException {
		String ext = super.getFileExt(file.getName());
		UploadObj obj = null;
		if (super.isImg(ext)) {
			obj = this.uploadFile(file, model_path);
			if (img_type == AbstractUploadFile.SMALL_IMG) {
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createSmallImg(f, wid, hig);
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
			}
			if (img_type == AbstractUploadFile.SUB_IMG) {
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createCutImg(f, x, y, wid, hig);
				obj.setSub_img_path(pathObj.getRealPath());
				// obj.setSub_img_path(super.createCutImg(f, x, y, wid, hig));
				obj.setRelativeSubImgPath(pathObj.getRelativePath());
			}
			if (img_type == AbstractUploadFile.MULTI_THUMBNAIL) {
				// 压缩多套图
				File f = new File(obj.getPath());
				UploadPathObj pathObj = super.createSmallImg(f, 90, 60, 1);
				obj.setSmall_img_path(pathObj.getRealPath());
				obj.setRelativeSmallImgPath(pathObj.getRelativePath());
				// 再压三套,用“wid_hig.jpg”来定义文件名
				super.createSmallImg(f, 150, 100, 1);
				super.createSmallImg(f, 300, 200, 1);
				super.createSmallImg(f, 600, 400, 1);
				super.createSmallImg(f, 800, 600, 1);
				// 测试small用代码，后续需要去掉
				super.createSmallImg(f, 150, 100);
			}

		}
		return obj;
	}

	@Override
	public UploadObj uploadImgFile(CommonsMultipartFile file, int length,
			int img_type, String model_path) throws IOException {
		UploadObj obj = null;
		UploadObj objnew = new UploadObj(file.getOriginalFilename());
		obj = this.uploadFile(file.getInputStream(),
				file.getOriginalFilename(), model_path);
		if (img_type == AbstractUploadFile.SMALL_IMG) {
			File f = new File(obj.getPath());
			UploadPathObj pathObj = super.createSmallImg(f, length);
			obj.setSmall_img_path(pathObj.getRealPath());
			obj.setRelativeSmallImgPath(pathObj.getRelativePath());
		} else if (img_type == AbstractUploadFile.SUB_IMG) {
			File f = new File(obj.getPath());
			UploadPathObj pathObj = super.createCutImg(f, 0, 0, length, length);
			obj.setSub_img_path(pathObj.getRealPath());
			obj.setRelativeSubImgPath(pathObj.getRelativePath());
		} else if (img_type == AbstractUploadFile.CUT_SMALL_IMG) {
			// 先切成正方形
			File f = new File(obj.getPath());
			Image img = ImageIO.read(f);
			int wid = img.getWidth(null);
			int heig = img.getHeight(null);
			UploadPathObj pathObj = null;
			if (wid > heig) {
				pathObj = super
						.createCutImg(f, (wid - heig) / 2, 0, heig, heig);
			} else {
				pathObj = super.createCutImg(f, 0, (heig - wid) / 2, wid, wid);
			}
			// 切成正方形的大图路径
			objnew.setPath(pathObj.getRealPath());
			objnew.setRelativePath(pathObj.getRelativePath());
			// 生成小图
			f = new File(objnew.getPath());
			pathObj = createSmallImg(f, length);
			objnew.setRelativeSmallImgPath(pathObj.getRelativePath());
			objnew.setSmall_img_path(pathObj.getRealPath());
		}
		return objnew;
	}

	@Override
	public UploadObj uploadorignalImgFile(CommonsMultipartFile file,
			String model_path) throws IOException {
		UploadObj obj = null;
		UploadObj objnew = new UploadObj(file.getOriginalFilename());
		obj = this.uploadFile(file.getInputStream(),
				file.getOriginalFilename(), model_path);
		// 原文件小图
		File f = new File(obj.getPath());
		Image img = ImageIO.read(f);
		int wid = img.getWidth(null);
		int heig = img.getHeight(null);
		String smallPath = createSmallImg(f.getParentFile().getPath(), img,
				wid, heig);
		String relativeSmallPath = smallPath.substring(Config.getString(
				"upload.path").length()
				+ "/upload".length());
		objnew.setSmall_img_path(smallPath);
		objnew.setRelativeSmallImgPath(relativeSmallPath);
		return objnew;
	}
}
